package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.operateunit;

import com.cqhilink.iot.jobstandardmanagement.dao.mapper.ReportTypeMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Organization;
import com.cqhilink.iot.jobstandardmanagement.facade.model.ReportItem;
import com.cqhilink.iot.jobstandardmanagement.facade.model.ReportType;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Ztree;
import com.cqhilink.iot.jobstandardmanagement.facade.service.operateunit.JsmReportItemService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.operateunit.JsmReportTypeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.TreeIconSkin;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄学乾 778142659@qq.com
 * @create 2017-08-25 8:36
 **/
public class JsmReportTypeServiceImpl implements JsmReportTypeService {
    public Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ReportTypeMapper reportTypeMapper ;

    @Autowired
    private JsmOrganizationService orgService ;

    @Autowired
    private  JsmReportItemService reportItemService ;
    /**
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> queryReportTypeAllList() throws Exception {
        List<ReportType> reportTypeList = reportTypeMapper.queryReportTypeAllList();
        List<Ztree> ztreeList =  offsetZtree(reportTypeList);
        return ztreeList ;
    }

    /**
     * @param reportTypeList 转换树形结构
     * @return
     * @throws Exception
     */
    @Override
    public List<Ztree> offsetZtree(List<ReportType> reportTypeList) throws Exception {
        List<Ztree> ztreeList =  new ArrayList<>() ;
        if(reportTypeList != null && reportTypeList.size() > 0){
            for(ReportType reportType : reportTypeList){
                Ztree ztree =  new Ztree() ;
                ztree.setId(reportType.getReportId());
                ztree.setName(reportType.getReportName());
                ztree.setpId(reportType.getParentReportName());
                ztree.setOpen(false);
                ztree.setIconSkin(TreeIconSkin.ICON_SERVICE_NODE.getDesc());
                ztreeList.add(ztree);
                List<ReportItem> reportItems = reportItemService.findReportItemList(reportType.getReportId());
                ztreeList.addAll(reportItemService.offsetReportItem(reportItems));
            }
        }

        // 设置 数据项

        return ztreeList;
    }

    /**
     * @param orgId 选择的组织机构
     */
    @Override
    public List<Ztree> findReportTypeList(String orgId,String keyword) throws Exception {
        logger.info("根据作业区查询作业区下的报表类型getReportTypeList-->" + orgId);
        List<Ztree> ztreeList = new ArrayList<>();
        Map<String,String> params = new HashMap<>();
        params.put("orgId",orgId);
        if(StringUtils.isNotEmpty(keyword)){
            params.put("keyword",keyword);
        }
        List<ReportType> reportTypes = reportTypeMapper.findReportTypeList(params);
        if (reportTypes != null && reportTypes.size() > 0) {
            for (ReportType reportType : reportTypes) {
                Ztree ztree = new Ztree();
                ztree.setId(reportType.getReportId());
                ztree.setName(reportType.getReportName());
                ztree.setIconSkin(TreeIconSkin.ICON_DEVICE_CHARA.getDesc());
                ztree.setIsParent(false);
                ztree.setOpen(true);
                ztree.setpId(reportType.getOrgId());
                ztreeList.add(ztree);
            }
        }
        // 组织机构数据
        Organization organization = orgService.selectOrgByOrgId(orgId);
        if(organization!=null && StringUtils.isNotEmpty(organization.getOrgId())){
            Ztree ztree = new Ztree();
            ztree.setId(organization.getOrgId());
            ztree.setName(organization.getOrgName());
            ztree.setIconSkin(orgService.offsetIconSkin(organization));
            ztree.setIsParent(false);
            ztree.setOpen(true);
            ztree.setpId(organization.getParentOrgId());
            ztreeList.add(ztree);
        }
        logger.info("根据作业区查询作业区下的报表类型getReportTypeList-->结束");
        return ztreeList;
    }
}
